const { Controller } = require("egg");

class AdminCOntroller extends Controller {
  // 商家注册
  async adminRegister() {
    const { ctx, service } = this;
    const { username, password } = ctx.request.body; //获取请求体信息
    //validate默认会将ctx.request.body作为第二个参数传进去
    // ctx.validate({ username: { type: "adminPhone", tips: "手机号格式错误" } });
    ctx.validate({ username: "phone", password: "password" });
    const res = await service.admininfo.adminRegiter(username, password);
    ctx.send(res);
    console.log(username, password);
  }
  //   商家登录
  async adminLogin() {
    const { ctx, service } = this;
    const { username, password } = ctx.request.body;
    ctx.validate({ username: "phone", password: "password" });
    const res = await service.admininfo.adminLogin(username, password);
    ctx.send(res);
  }
  // 更新商家信息
  async update() {
    const { ctx, service } = this;
    const body = ctx.request.body;
    const tips = {
      logo: "logo",
      name: "店铺名称",
      introduction: "店铺介绍",
      busunessTime: "营业时间",
      initialPrice: "外码起送价",
      address: "店铺地址",
    };
    const validateObj = {};
    Object.keys(body).forEach((field) => {
      validateObj[field] = { type: "nullValue", fieldName: tips[field] };
    });
    ctx.validate(validateObj);
    const res = await service.admininfo.adminUpdate(body);
    ctx.send(res);
  }
  // 获取商家信息
  async getMerchantInfo() {
    const { ctx } = this;
    const { _id } = ctx.query;
    ctx.validate({ _id: { type: "nullValue", fieldName: "_id" } }, ctx.query);
    const res = await ctx.model.Admininfo.findById(_id, {
      createdAt: 0,
      updatedAt: 0,
    });
    console.log(res);
    ctx.send({ data: res, msg: "成功" });
  }
   //   计算用户到商家的距离
   async distanceCalculator() {
    const { ctx, service } = this;
    const { _id, from } = ctx.query;
    ctx.validate(
      {
        _id: { type: "nullValue", fieldName: "_id" },
        from: { type: "nullValue", fieldName: "from" },
      },
      ctx.query
    );
    const res = await service.admininfo.distanceCalculator(_id, from);
    ctx.send(res);
  }
   //   计算用户到商家的距离
   async durationCaulcator() {
    const { ctx, service } = this;
    const { _id, from } = ctx.query;
    ctx.validate(
      {
        _id: { type: "nullValue", fieldName: "_id" },
        from: { type: "nullValue", fieldName: "from" },
      },
      ctx.query
    );
    const res = await service.admininfo.durationCaulcator(_id, from);
    ctx.send(res);
  }
}

module.exports = AdminCOntroller;
